package com.gdglc.spring.biz.impl.auth;

import java.util.Date;
import java.util.List;

import com.gdglc.spring.biz.auth.RoleBiz;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.y2.authprj.bean.auth.model.FunctionauthorityInfo;
import com.y2.authprj.bean.auth.model.RoleInfo;
import com.y2.authprj.bean.auth.model.RoleauthorityInfo;
import com.y2.authprj.bean.auth.model.StaffInfo;
import com.y2.authprj.bean.auth.model.SysroleInfo;
import com.y2.authprj.dao.auth.IRoleDao;
import com.y2.authprj.exception.BizException;

public class RoleBizImpl implements RoleBiz{

	private IRoleDao dao;
	
	public IRoleDao getDao() {
		return dao;
	}

	public void setDao(IRoleDao dao) {
		this.dao = dao;
	}

	@Override
	public List<RoleInfo> queryByChose(RoleInfo roleInfo) {
		return dao.queryByChose(roleInfo);
	}

	@Override
	public PageInfo<RoleInfo> findAllByName(String name, Integer pageNo, Integer pageSize) {
		pageNo = pageNo == null?1:pageNo;//当前页  *必须
	    pageSize = pageSize == null?10:pageSize;//每页的数量* 必须
	    PageHelper.startPage(pageNo, pageSize);//*必须
	    List<RoleInfo> list = this.findAllByName(name);
	    PageInfo<RoleInfo> page = new PageInfo<RoleInfo>(list);
	    return page;
	}

	@Override
	public List<RoleInfo> findAllByName(String name) {
		// TODO Auto-generated method stub
		return dao.findAllByName(name);
	}

	@Override
	public PageInfo<RoleInfo> queryByPage(RoleInfo roleInfo, Integer pageNo, Integer pageSize) {
		pageNo = pageNo == null?1:pageNo;//当前页  *必须
	    pageSize = pageSize == null?10:pageSize;//每页的数量* 必须
	    PageHelper.startPage(pageNo, pageSize);//*必须
	    List<RoleInfo> list = dao.queryByChose(roleInfo);
	    PageInfo<RoleInfo> page = new PageInfo<RoleInfo>(list);
	    return page;
	}

	@Override
	public void add(RoleInfo roleInfo) throws BizException {
		roleInfo.setDeleteFlag(1);
		roleInfo.setCreateTime(new Date());
		try{
			dao.add(roleInfo);
		}catch(Exception e){
			throw new BizException("添加失败",e);
		}
	}

	@Override
	public List<RoleInfo> findByRoleid(Integer roleId) throws BizException {
		if(null == roleId||roleId==0){
			throw new BizException("指定需要修改的记录");
		}
		//测试如果给一个不存在的id，userInfo是null还是一个新对象
		List<RoleInfo> roleInfo = dao.findByRoleid(roleId);
		if(null==roleInfo){
			throw new BizException("获取不到指定记录的信息，请确认是否已经被删除");
		}
		return roleInfo;
	}

	public void deleteRole(Integer roleauthorityId) throws BizException{
		if(null == roleauthorityId||roleauthorityId==0){
			throw new BizException("删除错误，请指定需要删除的记录");
		}
		try {
			dao.deleteRole(roleauthorityId);
		} catch (Exception e) {
			throw new BizException("删除失败",e);
		}
	}
	
}
